package adapteurMediaStore;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AudioColumns;

import com.insta.playdroid.metier.domain.Artist;

public class AdapteurArtistMediaStore implements IAdapteurMediaStore<Artist> {

	private Context context;

	public AdapteurArtistMediaStore(Context context) {
		this.context = context;

	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see adapteurMediaStore.IAdapteurMediaStore#collectAllValuesFromDB()
	 */
	@Override
	public List<Artist> collectAllValuesFromDB() {

		List<Artist> artists = new ArrayList<Artist>();
		int id;
		String artistName;

		// the cursor we will use to iterate over the db results
		Cursor cursor = null;

		// the list of columns our search relates to
		final String[] projection = new String[] {
				"distinct " + MediaStore.Audio.AudioColumns.ARTIST,
				MediaStore.Audio.AudioColumns.ARTIST_ID };

		// how we want to sort our results
		final String sortOrder = AudioColumns.ARTIST + " COLLATE LOCALIZED ASC";

		// the uri of the table that we want to query
		Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

		// launch the query
		cursor = context.getContentResolver().query(uri, projection, null,
				null, sortOrder);

		if (cursor != null) {
			artists = new ArrayList<Artist>(cursor.getCount());

			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {

				id = cursor.getInt(1);
				artistName = cursor.getString(0);
				artists.add(new Artist(id, artistName));

				cursor.moveToNext();
			}
		}
		return artists;

	}
}
